package com.hyjx.framework.component.codetable;

import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.common.tree.TreeObject;
import com.hyjx.framework.common.tree.TreeUtil;
import com.hyjx.framework.common.util.PinyinUtil;
import com.hyjx.framework.orm.dao.IBaseDao;
import com.hyjx.framework.orm.dao.Pager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 码表服务的实现类
 */
@Service
@Transactional
public class CodetableServiceImpl implements CodetableService {

    @Resource
    private IBaseDao dao;

    @Override
    public String queryList(CodetableModel model) {
        Pager pager = dao.getPager(model.getModuleName() + ".queryList", model.getModuleName() + ".queryCount", model);
        return CommonUtil.getPageJsonStr(pager);
    }

    @Override
    public boolean add(CodetableModel model) throws SQLException {
        dao.addObject(model.getModuleName() + ".insert", model);
        CodeTableManager.reloadCodeTable(model.getCodetable());
        return true;
    }

    @Override
    public CodetableModel getInfoById(CodetableModel model) {
        model = (CodetableModel) dao.queryObject(model.getModuleName() + ".getInfoById", model);
        return model;
    }

    @Override
    public int edit(CodetableModel model) {
        dao.updateObject(model.getModuleName() + ".update", model);
        CodeTableManager.reloadCodeTable(model.getCodetable());
        return 1;
    }

    @Override
    public int delete(CodetableModel model) {
        dao.deleteObject(model.getModuleName() + ".delete", model);
        CodeTableManager.reloadCodeTable(model.getCodetable());
        return 1;
    }

    @Override
    public String queryManagerList(CodeTableManagerModel model) {
        CodetableModel model1 = new CodetableModel();
        Pager pager = dao.getPager(model1.getModuleName() + ".querymanagerlist", model1.getModuleName() + ".querymanagercount", model);
        return CommonUtil.getPageJsonStr(pager);
    }

    @Override
    public String dealComboTree(String codetable, String parentcode, HttpServletResponse resp) {
        List<CodeBean> list;
        if (parentcode != null && !parentcode.equals("")) {
            list = CodeTableManager.getSubCodeList(codetable, parentcode);
        } else {
            list = CodeTableManager.getTableList(codetable);
        }
        List<TreeObject> toList = new ArrayList<TreeObject>();
        for (CodeBean cb : list) {
            TreeObject to = new TreeObject();
            to.setId(cb.getCode());
            to.setParent_id(cb.getParentCode());
            to.setText(cb.getName());
            toList.add(to);
        }
        String sb = TreeUtil.getEasyuiJsonStr(toList, "0", null);
        return sb;
    }

    @Override
    public String deal(String codetable, String parentcode, HttpServletResponse resp, String pinyin) {
        List<CodeBean> list;
        if (parentcode != null && !parentcode.equals("")) {
            list = CodeTableManager.getSubCodeList(codetable, parentcode);
        } else {
            list = CodeTableManager.getTableList(codetable);
        }
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        int i = 1;
        if (list != null)
            for (CodeBean bean : list) {
                if (!"1".equals(bean.getValidflag()))
                    continue;
                sb.append("{");
                sb.append("\"code\":\"" + bean.getCode() + "\",");
                String name = "";
                name = bean.getName();
                if (pinyin != null && !"".equals(pinyin))
                    name += " (" + PinyinUtil.getFirstChars(bean.getName()) + ")";

                sb.append("\"name\":\"" + name + "\"");
                sb.append("}");
                if (i != list.size()) {
                    sb.append(",");
                }
                i++;
            }
        sb.append("]");
        if (sb.toString().endsWith(",]"))
            sb.deleteCharAt(sb.toString().length() - 2);
        return sb.toString();
    }
}